import random
from glob import glob
from .ttable import name_pairs
from .trait import Trait

class GirlPics(object):
	def __init__(self, *args, **kw):
		object.__init__(self)
		self.profiles = []

	def scan(self, girl):
		str = "resources/characters/{}/[Pp]rofile*".format(
			girl.arch_name
		)
		self.profiles = glob(str)

	def random_profile(self):
		return random.choice(self.profiles)

#
# record to hold girl data
#
class Girl(object):
#
#	constructor is a little sparse
#	most of the fields are defined by the XML
#	although there are others I still need to
#	implement
#
	def __init__(self, *args, **kw):
		object.__init__(self)
		self.traits = []
		self.pics = GirlPics()
		self.tech = 10
		self.oral = 10
		self.arch_name = ""
		self._name = ""
		self.virgin = False
		self.jobs = [
			"Rest", "Rest", "Rest", "Rest",
		]

#
#	convenience methods - these let us sort on job fields
#	by column name
#
	@property
	def morning(self):      return self.jobs[0]
	@property
	def afternoon(self):    return self.jobs[1]
	@property
	def evening(self):      return self.jobs[2]
	@property
	def night(self):        return self.jobs[3]

#
#	the game labels are different to the field names
#	the field names are from the original WM
#	and are based on the girlpack XML. But the UI names
#	tend to get typed in on autopilot.
#
#	Hence: aliases!
#
#	Also, I may make these a little more complex than
#	simple aliases. Morale in particular should consider
#	more than simply happiness. On the other hand, why
#	further complicate things?
#
	@property
	def morale(self):
		return self.happiness

	@property
	def fatigue(self):
		return 100 - self.tiredness

#
#	add a trait to the trait list
#
	def add_trait(self, name):
		names = [x.name for x in self.traits]
		if name in names:
			return
		t = Trait(name=name)
		self.traits.append(t)

	def set_name(self, name, arch_name=None):
		self.name = name
		if arch_name:
			self.arch_name = arch_name
		else:
			self.arch_name = name

#
#	debug function to dump the data
#	"print" is a reserved word in python
#	so we call this print_
#
	def print_(self):
#
#		use the fields from  "name pairs" to drive the loop
#		since they're ordered and we don't need to worry
#		about what else may be in the dict
#
		for p in name_pairs:
			k = p[0]
			if k in [ "name", "desc" ]:
				v = self.__dict__[k].encode("utf-8")
			else:
				v = self.__dict__[k]
			print "{} : {}".format(k, v)
		for t in self.traits:
			print "Trait: {}".format(t.name)
		print ""

	def has_traits(self, names):
		for name in names:
			if not name in [ t.name for t in self.traits ]:
				return False
		return True

	def lacks_traits(self, names):
		for name in names:
			if name in [ t.name for t in self.traits ]:
				return False
		return True

	def calc_prices(self):
		print "*** CALCULATING PRICES for ", self.name
		self.ask_price = (self.beauty + self.charisma) / 2
		self.ask_price *= 0.6;
		self.ask_price += self.confidence / 10;
		self.ask_price += self.intelligence / 10;
		self.ask_price += self.fame / 2;
		self.ask_price += self.level * 10;

		self.ask_price *= random.uniform(0.8, 1.2) 
		self.ask_price = int(self.ask_price)
		print "ask_price = ", self.ask_price

		self.sale_price = self.ask_price * 15
		skills = self.anal
		skills += self.magic
		skills += self.bdsm
		skills += self.normal_sex
		skills += self.bestiality
		skills += self.group
		skills += self.lesbian
		skills += self.service
		skills += self.strip
		skills += self.combat
		self.sale_price += skills

		if self.virgin:
			self.sale_price *= 1.5
		self.sale_price *= random.uniform(0.8, 1.2) 
		self.sale_price = int(self.sale_price)
		print "sale_price = ", self.sale_price
